package com.zhuhjay.boot.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhuhjay.boot.mapper.MemberMapper;
import com.zhuhjay.boot.mapper.OrderMapper;
import com.zhuhjay.boot.mapper.OrdersettingMapper;
import com.zhuhjay.boot.mapper.SetmealMapper;
import com.zhuhjay.domain.entity.Member;
import com.zhuhjay.domain.entity.Order;
import com.zhuhjay.domain.entity.OrderSetting;
import com.zhuhjay.service.ReportService;
import com.zhuhjay.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZhuHJay
 * @date 2022/4/4 18:50
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public Map<String, Object> getBusinessReport() {
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 当前日期: 2022-04-04
        String reportDate = format.format(new Date());
        // 本月日期: 2022-04
        String thisMonth = reportDate.substring(0, reportDate.lastIndexOf("-"));
        // 本周周一的日期
        String thisWeekMonday = format.format(DateUtils.getThisWeekMonday(new Date()));

        // reportDate: null,           // 当前日期
        map.put("reportDate", reportDate);

        // todayNewMember: 0,          // 新增会员数
        Integer todayNewMember = memberMapper.selectCount(new QueryWrapper<Member>()
                .eq("regTime", reportDate));
        map.put("todayNewMember", todayNewMember);

        // totalMember: 0,             // 总会员数
        Integer totalMember = memberMapper.selectCount(null);
        map.put("totalMember", totalMember);

        // thisWeekNewMember: 0,       // 本周新增会员数
        Integer thisWeekNewMember = memberMapper.selectCount(new QueryWrapper<Member>()
                .ge("regTime", thisWeekMonday));
        map.put("thisWeekNewMember", thisWeekNewMember);

        // thisMonthNewMember: 0,      // 本月新增会员数
        Integer thisMonthNewMember = memberMapper.selectCount(new QueryWrapper<Member>()
                .like("regTime", thisMonth + "-%"));
        map.put("thisMonthNewMember", thisMonthNewMember);

        // todayOrderNumber: 0,        // 今日预约数
        Integer todayOrderNumber = ordersettingMapper.selectCount(new QueryWrapper<OrderSetting>()
                .eq("orderDate", reportDate));
        map.put("todayOrderNumber", todayOrderNumber);

        // todayVisitsNumber: 0,       // 今日到诊数
        Integer todayVisitsNumber = orderMapper.selectCount(new QueryWrapper<Order>()
                .eq("orderStatus", Order.ORDERSTATUS_YES));
        map.put("todayVisitsNumber", todayVisitsNumber);

        // thisWeekOrderNumber: 0,     // 本周预约数
        Integer thisWeekOrderNumber = orderMapper.selectCount(new QueryWrapper<Order>()
                .ge("orderDate", thisWeekMonday));
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);

        // thisWeekVisitsNumber: 0,    // 本周到诊数
        Integer thisWeekVisitsNumber = orderMapper.selectCount(new QueryWrapper<Order>()
                .eq("orderStatus", Order.ORDERSTATUS_YES)
                .ge("orderDate", thisWeekMonday));
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);

        // thisMonthOrderNumber: 0,    // 本月预约数
        Integer thisMonthOrderNumber = orderMapper.selectCount(new QueryWrapper<Order>()
                .like("orderDate", thisMonth + "-%"));
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);

        // thisMonthVisitsNumber: 0,   // 本月到诊数
        Integer thisMonthVisitsNumber = orderMapper.selectCount(new QueryWrapper<Order>()
                .eq("orderStatus", Order.ORDERSTATUS_YES)
                .like("orderDate", thisMonth + "-%"));
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);

        /*
        {
            hotSetmeal: [               // 热门套餐
                {
                    name: '',            // 套餐名称
                    setmeal_count: 200,  // 预约数量
                    proportion: 0.222    // 占比
                }
            ]
        }
        */
        List<Map<String, Object>> hotSetmeal = setmealMapper.findHotSetmeal();
        map.put("hotSetmeal", hotSetmeal);

        return map;
    }
}
